package xuegao.study.mall.admin.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xuegao.study.mall.admin.common.CommonPage;
import xuegao.study.mall.admin.service.pms.PmsProductAttributeCategoryService;
import xuegao.study.mall.admin.service.pms.PmsProductAttributeService;
import xuegao.study.mall.common.api.CommonResult;
import xuegao.study.mall.common.dto.PmsProductAttributeCategoryItem;
import xuegao.study.mall.common.model.pms.PmsProductAttributeCategory;

import java.util.List;

/**
 * 商品属性分类管理
 */
@RestController
@RequestMapping("/productAttribute/category")
public class PmsProductAttributeCategoryController {

    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;

    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    @GetMapping("/list")
    public CommonResult<CommonPage<PmsProductAttributeCategory>> queryCategoryList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                                   @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        List<PmsProductAttributeCategory> list = pmsProductAttributeService.categroyList(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation("获取所有商品属性分类及其下属性")
    @GetMapping(value = "/list/withAttr")
    public CommonResult<List<PmsProductAttributeCategoryItem>> getListWithAttr() {
        List<PmsProductAttributeCategoryItem> list = pmsProductAttributeCategoryService.getWithAttr();
        return CommonResult.success(list);
    }

    @PostMapping("/add")
    public CommonResult<Integer> addCategory(@RequestParam String name) {
        int count = pmsProductAttributeService.addCategory(name);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @PostMapping("/update/{id}")
    public CommonResult<Integer> updateCategory(@PathVariable("id") Long id, @RequestParam String name) {
        int count = pmsProductAttributeService.updateCategory(id, name);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @DeleteMapping("/delete/{id}")
    public CommonResult<Integer> deleteCategory(@PathVariable("id") Long id) {
        int count = pmsProductAttributeService.deleteCategoryById(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
